package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.model.OrderBackRecord;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.StringUtils;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.service.IOrderBackRecordService;
import com.stylefeng.guns.modular.system.service.ITOrderService;
import com.stylefeng.guns.modular.system.warpper.OrderWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 回单控制器
 *
 * @author wengys
 * @Date 2018-03-12
 */
@Controller
@RequestMapping("/orderBack")
public class OrderBackController extends BaseController {

    private String PREFIX = "/system/tOrder/orderBack/";

    @Autowired
    private ITOrderService tOrderService;

    @Autowired
    private IOrderBackRecordService iOrderBackRecordService;
    /**
     * 跳转到回单首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "orderBack.html";
    }

    @RequestMapping("orderBackRecord")
    public String orderBackRecord() {
        return PREFIX + "orderBackRecord.html";
    }

    /**
     * 回单接收页面
     */
    @RequestMapping("/receive")
    public String receive(Model model) {
        model.addAttribute("ids",getPara("ids"));
        return PREFIX + "receive.html";
    }

    /**
     * 回单发送页面
     */
    @RequestMapping("/send")
    public String send(Model model) {
        model.addAttribute("ids",getPara("ids"));
        return PREFIX + "send.html";
    }
    /**
     * 一键收发页面
     */
    @RequestMapping("/receAndSend")
    public String receAndSend(Model model) {
        model.addAttribute("ids",getPara("ids"));
        return PREFIX + "receAndSend.html";
    }

    @RequestMapping(value = "/exportOrderBackist")
    @ResponseBody
    public Object exportOrderBackist() {
        Page<Map<String, Object>> page = new Page(0, 50000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String params[]=(getPara("name")==null?"":getPara("name")).split("；");// 0 万能查询 2 上游 3 地址
        Map m=new HashMap();
        m.put("dot_id",ShiroKit.getUser().getDotId());
        m.put("name",params[0]);
        if(params.length>1){
            m.put("send_dept_name",params[1]);
        }
        if(params.length>2){
            m.put("address",params[2]);
        }
//        m.put("name",getPara("name"));
        m.put("type",getPara("type"));//订单类型 中转 自提 配送
        m.put("is_out",getPara("is_out"));//是否失联
        if( ToolUtil.isEmpty(getPara("receipt_status"))){
            if(!ToolUtil.isEmpty(getPara("name"))){
                m.put("receipt_status", "");//回单要求
            }else{
                m.put("receipt_status", ToolUtil.isEmpty(getPara("receipt_status"))==true?"0":getPara("receipt_status"));//回单要求
            }
        }else{
            m.put("receipt_status", ToolUtil.isEmpty(getPara("receipt_status"))==true?"0":getPara("receipt_status"));//回单要求
        }

        m.put("beginTime",getPara("beginTime"));
        m.put("endTime",getPara("endTime"));
        List<Map<String, Object>> orders = tOrderService.selectOrderList(page,m);

        String dirPath = HttpKit.getRequest().getSession().getServletContext().getRealPath("/") + Const.FILE_PATH;
        String dateDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

        File dir = new File(dirPath + "/" + dateDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String datePath = "/回单列表"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xls";
        tOrderService.exportOrderBack(dirPath + "/" + dateDir+datePath,"/"+Const.FILE_PATH+"/" + dateDir+datePath,orders);
        return renderFile("回单列表"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xls",dirPath + "/" + dateDir+datePath);
    }

    /**
     * 获取回单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list( ) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String params[]=(getPara("name")==null?"":getPara("name")).split("；");// 0 万能查询 2 上游 3 地址
        Map m=new HashMap();
        m.put("dot_id",ShiroKit.getUser().getDotId());
        m.put("name",params[0]);
        if(params.length>1){
            m.put("send_dept_name",params[1]);
        }
        if(params.length>2){
            m.put("address",params[2]);
        }
//        m.put("name",getPara("name"));
        m.put("type",getPara("type"));//订单类型 中转 自提 配送
        m.put("is_out",getPara("is_out"));//是否失联
        if( ToolUtil.isEmpty(getPara("receipt_status"))){
            if(!ToolUtil.isEmpty(getPara("name"))){
                m.put("receipt_status", "");//回单要求
            }else{
                m.put("receipt_status", ToolUtil.isEmpty(getPara("receipt_status"))==true?"0":getPara("receipt_status"));//回单要求
            }
        }else{
            m.put("receipt_status", ToolUtil.isEmpty(getPara("receipt_status"))==true?"0":getPara("receipt_status"));//回单要求
        }

        m.put("beginTime",getPara("beginTime"));
        m.put("endTime",getPara("endTime"));
        List<Map<String, Object>> orders = tOrderService.selectOrderList(page,m);
        pageInfoBT.setRows(orders);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/orderBackRecordList")
    @ResponseBody
    public Object orderBackRecordList(Integer limit, Integer offset    ) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("dot_id",ShiroKit.getUser().getDotId());
//        m.put("name",getPara("name"));
//        m.put("type",getPara("type"));//订单类型 中转 自提 配送
//        m.put("is_out",getPara("is_out"));//是否失联
//        m.put("receipt_status",getPara("receipt_status"));//回单要求
        List<Map<String, Object>> orders = iOrderBackRecordService.selectOrderBackRecordList(page,m);
        pageInfoBT.setRows(orders);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 回单接收
     */
    @RequestMapping(value = "/receive_add")
    @ResponseBody
    public Object receive_add() throws ParseException {
        String receive_time=getPara("receive_time");
        String sender=getPara("sender");
        String remark=getPara("remark");
        String ids=getPara("ids");
        OrderBackRecord orderBackRecord=new OrderBackRecord();
        orderBackRecord.setSender(sender);
        orderBackRecord.setRemark(remark);
        orderBackRecord.setType(1);//接收
        orderBackRecord.setDot_id(ShiroKit.getUser().getDotId());
        orderBackRecord.setRecord_no("HD"+ShiroKit.getUser().getDotId()+DateUtil.getAllTimes());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(receive_time);//提取格式中的日期
        orderBackRecord.setReceiveTime(date);
        iOrderBackRecordService.insertReceive(ids,orderBackRecord);
        return super.SUCCESS_TIP;
    }

    /**
     * 回单发送
     */
    @RequestMapping(value = "/send_add")
    @ResponseBody
    public Object send_add() throws ParseException {
        String send_time=getPara("send_time");
        String receiver=getPara("receiver");
        String remark=getPara("remark");
        String process_method=getPara("process_method");
        String ids=getPara("ids");

        OrderBackRecord orderBackRecord=new OrderBackRecord();
        orderBackRecord.setReceiver(receiver);
        orderBackRecord.setRemark(remark);
        orderBackRecord.setProcessMethod(Integer.parseInt(process_method));
        orderBackRecord.setDot_id(ShiroKit.getUser().getDotId());
        orderBackRecord.setRecord_no("HD"+ShiroKit.getUser().getDotId()+DateUtil.getAllTimes());
        orderBackRecord.setType(2);//发送

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(send_time);//提取格式中的日期
        orderBackRecord.setSendTime(date);
        iOrderBackRecordService.insertSend(ids,orderBackRecord);
        return super.SUCCESS_TIP;
    }
    /**
     * 一键收发
     */
    @RequestMapping(value = "/receAndSendAdd")
    @ResponseBody
    public Object receAndSendAdd() throws ParseException {
        String send_time=getPara("send_time");
        String receive_time=getPara("receive_time");
        String receiver=getPara("receiver");
        String remark=getPara("remark");
        String process_method=getPara("process_method");
        String ids=getPara("ids");
        String sender=getPara("sender");


        OrderBackRecord orderBackRecord=new OrderBackRecord();
        orderBackRecord.setReceiver(receiver);
        orderBackRecord.setSender(sender);
        orderBackRecord.setRemark(remark);
        orderBackRecord.setProcessMethod(Integer.parseInt(process_method));
        orderBackRecord.setType(3);//一键收发

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf1.parse(send_time);//提取格式中的日期
        orderBackRecord.setSendTime(date1);

        orderBackRecord.setDot_id(ShiroKit.getUser().getDotId());
        orderBackRecord.setRecord_no("HD"+ShiroKit.getUser().getDotId()+DateUtil.getAllTimes());

        Date date2= sdf1.parse(receive_time);//提取格式中的日期
        orderBackRecord.setReceiveTime(date2);
        iOrderBackRecordService.insertReceiveAndSend(ids,orderBackRecord);
        return super.SUCCESS_TIP;
    }

    /**
     * 导出
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public Object export(HttpServletRequest request, @RequestParam(required = false) String id, @RequestParam(required = false) String is_normal) {
        String dirPath = request.getSession().getServletContext().getRealPath("/") + Const.FILE_PATH;
        String dateDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

        File dir = new File(dirPath + "/" + dateDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String datePath = "/"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xlsx";
        iOrderBackRecordService.export(dirPath + "/" + dateDir+datePath,"/"+Const.FILE_PATH+"/" + dateDir+datePath,id);
        return renderFile(new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xlsx",dirPath + "/" + dateDir+datePath);
    }

}
